/**
 * 
 */
package com.test.activityMq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import com.test.activityMq.listener.MqListener;

/**
 * @author liya
 * 上午10:43:34
 */
public class ActivityReceive {
	
	private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
	private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
	private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;
	
	public static void main(String args[]){
		ConnectionFactory connectionFactory; //连接工厂
		Connection connection = null; //连接
		Session session; //会话 接受或者发送消息的线程
		Destination destination; //消息目的地
		MessageConsumer messageConsumer; //消息消费者
		
		connectionFactory = new ActiveMQConnectionFactory(ActivityReceive.USERNAME, ActivityReceive.PASSWORD, ActivityReceive.BROKERURL);
		
		try {
			connection = connectionFactory.createConnection(); //通过连接工厂创建连接
			connection.start(); //启动连接
			session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); //创建会话
			destination = session.createQueue("FirstQueue1"); //创建连接的消息队列
			messageConsumer = session.createConsumer(destination); //创建消息消费者
			//方法一  不推荐
			/*while (true) {
				TextMessage message = (TextMessage)messageConsumer.receive(100000);
				if(message!=null){
					System.out.println("收到的消息 : "+message.getText());
				}else{
					break;
				}
			}*/
			
			//方法二 
			messageConsumer.setMessageListener(new MqListener()); //注册消息监听器
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
